package com.jugheadzhou.mqtt.consumer.handler;

import com.jugheadzhou.mqtt.consumer.service.MachineStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 消息处理器
 *
 * @author : jugheadzhou
 */
@Slf4j
@Service
public class MqttTopicHandler {

    @Autowired
    private MachineStatusService sysHeapService;

    /**
     * 这一步主要是对消息的有效荷载进行非空校验以及日志打印
     */
    public void handler(String topic, String payload) {
        log.info("默认MQTT消息处理：{}-{}", topic, payload);
        try {
            switch (topic) {
                //MDC设备状态
                case "mdc-machine-status":
                    sysHeapService.handleStatus(payload);
                    break;
                //测试
                case "test":
                    log.info("MQTT测试消息：{}-{}", topic, payload);
                    break;
                default:
                    log.warn("未知topic：{}的消息，请检查您的配置！", topic);
                    break;
            }
            log.info("来自topic:{}的消息处理完毕！", topic);
        } catch (Exception e) {
            log.error("来自topic:{}的消息处理异常！原因:{}", topic, e);
        }
    }

}
